<?php
namespace app\daili\controller;
use app\dsadmin\model\MemberModel;
use app\common\model\EnameModel;
use think\Db;
use think\Cache;

class Tuiguang extends Base
{
    public function index()
    {
        $key = input('key');
        // $lists = Db::name('member')->where(['status' => 1,'ename_first' => "www.jiujiucar.com.cn"])->select();
        
        // foreach ($lists as $v){
            
        //     // if(!empty($v['password'])){
        //     //      $members = Db::name('member')->where(['id' => $v['id']])->update(['password' => md5($v['password'])]);
        //     // }
            
        //     $day   = date("Y-m-d");
        //     $zuori = date("Y-m-d", strtotime("$day-1 day"));
        //     $mapzf['shijian'] = ['like', "%" . $zuori . "%"];
        //     $mapzf['userid']  = $v['id'];
        //     $money2 = Db::name("dingdan")->where($mapzf)->sum('money');
            
        //     if($money2 <= 500)
        //     {
        //         if(1 == rand(1,5)){
        //              $ename = 'www.sccdsdz008.cn';
        //              Db::name('member')->where(['id' => $v['id']])->update(['ename_first' => $ename]);
        //         }
                
        //         // if(1 == rand(1,5)){
        //         //      $ename = 'www.sunjianming.cn';
        //         //      Db::name('member')->where(['id' => $v['id']])->update(['ename_first' => $ename]);
        //         // }
                
               
        //     }
        // }
        // var_dump(111);
        // die;

        $map['closed'] = 0; //0δɾ����1��ɾ��
        if ($key && $key !== "")
        {
            $map['id|account|nickname|qq|weixin'] = ['=', $key ];
        }
        $user_id = session('dailiuid');
        
        $map = ['zong_id' => $user_id];
        
        $jin     = date('Y-m-d H:i:s',strtotime(date("Y-m-d")));
        $jintime = date('Y-m-d H:i:s',time());
        $zuo     = date('Y-m-d H:i:s',strtotime(date("Y-m-d",strtotime("-1 day"))));
        
        
        $wherej['shijian'] = ['between',$jin .','.$jintime];
        $wherejz['shijian'] = ['between',$zuo .','.$jin];

        $member = new MemberModel();

        $Nowpage = input('get.page') ? input('get.page'):1;

        $limits = 10; 
        $count = $member->getAllCount($map);
        
        $allpage = intval(ceil($count / $limits));

        $lists = $member->getMemberByWhere($map, $Nowpage, $limits);

        $this->assign('Nowpage', $Nowpage); 
        $this->assign('allpage', $allpage); 
        $this->assign('val', $key);
       
        if (input('get.page'))
        {
             foreach ($lists as &$v){
                $wherej['userid'] =  $v['id'];
                $wherejz['userid'] =  $v['id'];
                $money = Db::name('dingdan')->where($wherej)->sum('money');
                $moneyz = Db::name('dingdan')->where($wherejz)->sum('money');
                $v['last_login_time'] = date('Y-m-d H:i:s',$v['last_login_time']);
                $v['money'] = $money;
                $v['moneyz'] = $moneyz;
                unset($v['password']);
                unset($v['txpassword']);
            }
            unset($v);
            
            return json($lists);
        }
        return $this->fetch();
    }
    
   

    public function edit()
    {
        $id   = input('id');
       
        $tui  = Db::name("dwz")->where(['userid' => session('dailiuid'), 'id' => $id])->find();
        $tui['url'] = $tui['tui'];
        if (input('post.'))
        {
            $id = input('id/d');
            
            $shipinurl = input('post.shipinurl');
            
            
            
            Db::name("dwz")->where(['userid' => session('dailiuid'), 'id' => $id])->update(['tui' => $shipinurl]);
            
            
            $ddh2 = $tui['ddh'];
            $info = Db::name('dwz')->where(['userid' => session('dailiuid'), 'id' => $id])->find();
		    Cache::store('redis')->set('qtdwz'.$ddh2,json_encode($info),7879680);
           
            return json(['code' => 1, 'msg' => "修改成功"]);
        }

        $this->assign('tui', $tui);
        
        $ddhtz = random(10);
       
            
        return $this->fetch();
    }
    
   

    // public function add()
    // {
    //     if (input('post.'))
    //     {
            
    //         $shipinurl = input('post.shipinurl');
            
    //         if(empty($shipinurl)){
    //             $shipinurl = 0;
    //         }
    //         $id = Db::name("tui")->insertGetId(['url' => $shipinurl, 'userid' => session('dailiuid')]);
            
    //         $type = "w2cj";
           
           
    //         if($type == "w2cj"){
                
    //             $ddhtz   = random(10);
        
    //             $member = Db::name('member')->where(['id' => session('dailiuid')])->find();
                
    //             $ename  = $member['ename_first'] ;
                
                
    //             if($ename == "www.fubodi.cn" || $ename == "www.ptransaction.cn"){
    //                 $json_url = "http://120.77.247.20:2222/index/index/dwz_w2cj";
    //             }
            
    //             if($ename == "www.catexpo.com.cn" || $ename == "www.youranlife.cn" || $ename == "hbznxl.com" || $ename == "www.daimaomao.cn")
    //             {
    //                 $json_url = "http://120.77.247.20:80/index/index/dwz_w2cj";
                  
    //             }
                
    //             if($member['id'] == 10027){
                    
    //                  if($ename == "www.fubodi.cn" || $ename == "www.ptransaction.cn"){
    //                      $json_url = "http://120.77.247.20:2222/index/index/dwz_w2cj";
    //                 }
                
    //                 if($ename == "www.catexpo.com.cn" || $ename == "www.youranlife.cn" || $ename == "hbznxl.com" || $ename == "www.daimaomao.cn")
    //                 {
    //                     $json_url = "http://120.77.247.20:80/index/index/dwz_w2cj";
                      
    //                 }
    //             }
            
    //             $user_id = session('dailiuid');
    //             $string = $user_id.','.$ddhtz.','.$id;
    //             $jiami  = lock_url($string);
            
    //             $json_data['domain']      = "/index/index/hezi?userid=".$jiami."&ddh=".$ddhtz."&id=".$id;
    //             $json_data['platformId']  = 6;
    //             $json_data['secretKey']   = "e10adc3949ba59abbe56e057f20f883e";
    //             $json_data['mainDomain']  = $ename;
    //             $json  = json_encode($json_data);
               
    //             if(strpos($jiami,'%') !== false){ 
    //                 return json(['code' => 0, 'msg' => "添加失败"]);
    //             }
    //             $res   = get_duan_url($json_url,$json);
                
    //             $res_r = json_decode($res,true);
                
    //             // var_dump(bin2hex($res_r['message']));
                
    //             if($res_r['status'] == 200){
                   
    //                 $url = $res_r['message'];
    //                 $dwzd['userid']         = $user_id;
    //                 $dwzd['dwz']            = $url;
    //                 $dwzd['ddh']            = $ddhtz;
    //                 $dwzd['sid']            = $id;
    //                 $dwzd['status']          = 1;
    //                 $dwzd['jiami']          = $jiami;
    //                 $dwzd['tui']            = $shipinurl;
    //                 $dwzd['create_time']    = time();
                    
    //                 Db::name('dwz')->insert($dwzd);
                    
    //                 Cache::store('redis')->set($jiami,json_encode($dwzd),5253120);
                       
    //             }else{
    //                 return json(['code' => 0, 'msg' => "添加失败"]);
    //             }
           
    //     }
           
    //       return json(['code' => 1, 'msg' => "添加成功"]); 
         
    //     }

    //     return $this->fetch();
    // }
    
    
   public function add()
    {
        // die("升级维护中...");
        $dwz_type  = input('dwz_type');
        if (input('post.'))
        {
            
            $shipinurl = input('post.shipinurl');
            
            $dwz_type  = input('dwz_type');
             
            $xiaoshi   = input('xiaoshi/d',0);
            
            
            if($xiaoshi > 0){
                $times = $xiaoshi * 3600;
                
                if($times >= 604800){
                    return json(['code' => 0, 'msg' => "小时数不能大于168小时"]);
                }
            }
            
            if(session('dailiuid') == 1001200 ){
                  return json(['code' => 0, 'msg' => "因系统升级已转移到内部台,请联系客服"]); 
            }
            
           
             
             $kapian_status = Db::name('member')->where(['id' => session('dailiuid')])->value('kapian_status');
            
             if($dwz_type == 1 && $kapian_status == 0){
                 return json(['code' => 0, 'msg' => "暂未开放"]); 
             }
             if(empty($shipinurl)){
                $shipinurl = 0;
             }
            $id = Db::name("tui")->insertGetId(['url' => $shipinurl, 'userid' => session('dailiuid')]);
            
            $type = "w2cj";
           
        if( $type == "w2cj" ){
    
            $ddhtz   = random(10);
    
            $member = Db::name('member')->where(['id' => session('dailiuid')])->find();
            
            $ename = $member['ename_first'];
                    
            $fangfeng_type = Db::name('ename')->where(['ename' => $ename])->find();
            
            $fangfeng_type = $fangfeng_type['fangfeng_type'];
            
            if($fangfeng_type == 666){
                $json_url = "http://120.26.164.1:1666/index/index/dwz_w2cj1";
            }
            
            $user_id = session('dailiuid');
         
            $string = $user_id.','.$ddhtz.','.$id;
            $jiami  = lock_url($string);
            
            
            // $json_data['domain']      = "/index/index/hezi?userid=".$jiami."&ddh=".$ddhtz."&id=".$id;
            
            // if($user_id = 201168){
                
            // }
            $cneame_url = get_random(rand(16,32),"QAZWSXEDCRFVTGBYHNUJMIKOLPqazwsxedcrfvtgbyhnujmiklop1234567890");
            
            $json_data['domain']      = "/$cneame_url.html?userid=".$jiami."&ddh=".$ddhtz."&id=".$id;
            
            $json_data['platformId']  = 6;
            $json_data['secretKey']   = "e10adc3949ba59abbe56e057f20f883e";
            $json_data['mainDomain']  = $ename;
            $json  = json_encode($json_data);
            
            $res   = get_duan_url($json_url,$json);
            
            $res_r = json_decode($res,true);
            
            $stream_opts = [
                "ssl" => [
                    "verify_peer"=>false,
                    "verify_peer_name"=>false,
                ]
            ]; 
            
            $pla                 = [];
            $pla['ddh']          = $ddhtz;
            $pla['userid']       = $jiami;
            $pla['id']           = $id;
            $pla['short_domian'] = $res_r['message'];
            
            // if(session('dailiuid') == "201168"){
                
            // }
            
            
            
            //第一种短链接
            $api_curl_1 = Db::name('lianjie')->where(['dwz_type'=>'alb_dwz','state'=>1])->limit(1)->order('rand()')->find();
            
            if($api_curl_1){
                $api_name = $api_curl_1['type'];
                $data1 = null;
                if($api_name == "hangzhou_dwz"){
                    $data1 = $this->hangzhou_dwz($api_curl_1,$pla);
                }
            }
            
            
            //第二种短链接
            // $api_curl_2 = Db::name('lianjie')->where(['dwz_type'=>'txy_dwz','state'=>1])->limit(1)->order('rand()')->find();
            // if($api_curl_2){
            //     $api_name = $api_curl_2['type'];
            //     $data2 = null;
            //     if($api_name == "xianggang_dwz"){
            //         $data2 = $this->xianggang_dwz($api_curl_2,$pla);
            //     }
            // }
            
            
            
            //第三种短链接
            $api_curl_3 = Db::name('lianjie')->where(['dwz_type'=>'txy_cso_dwz','state'=>1])->limit(1)->order('rand()')->find();
            if($api_curl_3){
                $api_name = $api_curl_3['type'];
                $data3 = null;
                if($api_name == "cos_dwz"){
                    $data3 = $this->cos_dwz($api_curl_3,$pla);
                }
            }
            
            //第四种短链接
            $api_curl_4 = Db::name('lianjie')->where(['dwz_type'=>'cunchu_dwz','state'=>1])->limit(1)->order('rand()')->find();
            if($api_curl_4){
                $api_name = $api_curl_4['type'];
                $data4 = null;
                if($api_name == "cos_dwz"){
                    $data4 = $this->cos_dwz($api_curl_4,$pla);
                }
            }
            
            //第五种短链接
            // $api_curl_5 = Db::name('lianjie')->where(['dwz_type'=>'upcdn_dwz','state'=>1])->limit(1)->order('rand()')->find();
            // if($api_curl_5){
            //     $api_name = $api_curl_5['type'];
            //     $data5 = null;
            //     if($api_name == "xianggang_dwz"){
            //         $data5 = $this->xianggang_dwz($api_curl_5,$pla);
            //     }
            // }
            
            //第六种短链接
            // $api_curl_6 = Db::name('lianjie')->where(['dwz_type'=>'kunlunea_dwz','state'=>1])->limit(1)->order('rand()')->find();
            // if($api_curl_6){
            //     $api_name = $api_curl_6['type'];
            //     $data6 = null;
            //     if($api_name == "shenzheng_dwz"){
            //         $data6 = $this->shenzheng_dwz($api_curl_6,$pla);
            //     }
            // }
            
            //第七种短链接
            $api_curl_7 = Db::name('lianjie')->where(['dwz_type'=>'qqjs_dwz_1','state'=>1])->limit(1)->order('rand()')->find();
            if($api_curl_7){
                $api_name = $api_curl_7['type'];
                $data7 = null;
                if($api_name == "xianggang_dwz"){
                    $data7 = $this->xianggang_dwz($api_curl_7,$pla);
                }
            }
            
            //第八种短链接
            $api_curl_8 = Db::name('lianjie')->where(['dwz_type'=>'qqjs_dwz_2','state'=>1])->limit(1)->order('rand()')->find();
            if($api_curl_8){
                $api_name = $api_curl_8['type'];
                $data8 = null;
                if($api_name == "xianggang_dwz"){
                    $data8 = $this->xianggang_dwz($api_curl_8,$pla);
                }
            }
            
            
            //第九种短链接
            // $api_curl_9 = Db::name('lianjie')->where(['dwz_type'=>'baidu_dwz','state'=>1])->limit(1)->order('rand()')->find();
            // if($api_curl_9){
            //     $api_name = $api_curl_9['type'];
            //     $data9 = null;
            //     if($api_name == "api_dwz"){
            //         $data9 = $this->api_dwz($api_curl_9,$pla);
            //     }
            // }
            
            
            // var_dump($data2);
            // var_dump($data3);
            // var_dump($data4);
            // var_dump($data5);
            // var_dump($data6);
            // var_dump($data7);
            // var_dump($data8);
            // var_dump($data9);
            
            
            
            
            
            if($data7['code'] == 1){
                $url  = $res_r['message'];
                $url1 = $res_r['messaget'];
                $dwzd['userid']         = $user_id;
                $dwzd['dwz']            = $url;
                $dwzd['dwz1']           = $url1;
                $dwzd['ddh']            = $ddhtz;
                $dwzd['sid']            = $id;
                $dwzd['jiami']          = $jiami;
                $dwzd['status']         = 1;
                
                $dwzd['kdwz1']          = $data1['short'];
                $dwzd['sdwz2']          = "";
                $dwzd['ddwz3']          = $data3['short'];
                $dwzd['ydwz4']          = $data4['short'];
                $dwzd['udwz5']          = $data8['short'];
                $dwzd['ldwz6']          = $data8['short'];
                $dwzd['linkdwz7']       = $data7['short'];
                $dwzd['gmldwz8']        = $data8['short'];
                $dwzd['apidwz9']        = "";
                
                $dwzd['create_time']    = time();
                $dwzd['tui']            = $shipinurl;
                $dwzd['dwz_type']       = $dwz_type;
                $dwzd['xiaoshi']        = $xiaoshi;
                Db::name('dwz')->insert($dwzd);
                
                Cache::store('redis')->set($jiami,json_encode($dwzd),5253120);
                   
            }else{
                return json(['code' => 0, 'msg' => "添加失败"]);
            }
                   
        }else{
             return json(['code' => 0, 'msg' => "添加失败"]); 
        }
        
           return json(['code' => 1, 'msg' => "添加成功"]); 
         
        }
        $this->assign('dwz_type', $dwz_type);

        return $this->fetch();
    }
     
    public function api_dwz($api,$pla){
        
        $geturl = $api['lianjie'];
        
        // if($geturl == "t.cn"){
        //     $type = "tcn1";
        // }else if($geturl == "sinaurl.cn"){
        //     $type = "sinaurl1";
        // }else if($geturl == "dd.ma"){
        //     $type = "ddma";
        // }else if($geturl == "mi.mbd.baidu.com"){
        //     $type = "mibaidu";
        // }else if($geturl == "my.mbd.baidu.com"){
        //     $type = "mybaidu";
        // }else{
        //     $type = "tcn1";
        // }
        $real_url = $pla['short_domian'];
        $real_url = urlencode($real_url);
        
        $get_url = "http://0720.okeytag.com/api/url.php?type=tcn1&pattern=1&token=f9cc3daf4ccb42eb6428166a7169f421&url=$real_url";
        // $get_url = "http://dwz.zzxutf.cn/api/build2?key=gdyka5boz&format=json&url=".urlencode($real_url);
        
        $stream_opts = [
            "ssl" => [
                "verify_peer"=>false,
                "verify_peer_name"=>false,
            ]
        ];
        
        $resdata = file_get_contents($get_url); 
        
        $resdata = json_decode($resdata,true); 
        
        $res['code']=-1;
        $res['short']='';
        $res['msg'] = '';
        
        if($resdata['code'] == 200){
            $res['code']=1;
            $res['short'] = $resdata['dwz'];
            $res['msg'] = '成功';
        }else{
            $res['code']=-1;
            $res['msg'] = "错误";
        }
        return $res;
    }
    
    
    public function shenzheng_dwz($api,$pla){
        
        $geturl = $api['lianjie'];
        $real_url = $pla['short_domian'];
        $get_url = "http://cdn1.wyjqqa.cn.w.kunlunea.com/api/build2?key=gdyka5boz&format=json&url=".urlencode($real_url);
        
        $stream_opts = [
            "ssl" => [
                "verify_peer"=>false,
                "verify_peer_name"=>false,
            ]
        ]; 
        
               
        $url     = file_get_contents($get_url,false, stream_context_create($stream_opts));
        // var_dump($url);
        // die;
        $resdata  = json_decode($url,true);
        
        $resdata['short'] = substr($resdata['short'],36);
        
        
        $resdata['short'] = "http://".$geturl.$resdata['short'];
        
        $res['code']=-1;
        $res['short']='';
        $res['msg'] = '';
        if($resdata['error'] == 0){
            
            $res['code']=1;
            $res['short'] = $resdata['short'];
            $res['msg'] = '成功';
        }else{
            $res['code']=-1;
            $res['msg'] = "错误";
        }
        return $res;
    }
    
    public function hangzhou_dwz($api,$pla){
        
        $geturl = $api['lianjie'];
        $real_url = $pla['short_domian'];
        $get_url = "http://alb-mbd7cwtaryugc7adq4.cn-hangzhou.alb.aliyuncs.com/api/build2?key=gdyka5boz&format=json&url=".urlencode($real_url);
        
        $stream_opts = [
            "ssl" => [
                "verify_peer"=>false,
                "verify_peer_name"=>false,
            ]
        ]; 
        
               
        $url     = file_get_contents($get_url,false, stream_context_create($stream_opts));
        
        $resdata  = json_decode($url,true);
        
        $resdata['short'] = substr($resdata['short'],58);
        
        
        $resdata['short'] = "http://".$geturl.$resdata['short'];
        
        $res['code']=-1;
        $res['short']='';
        $res['msg'] = '';
        if($resdata['error'] == 0){
            
            $res['code']=1;
            $res['short'] = $resdata['short'];
            $res['msg'] = '成功';
        }else{
            $res['code']=-1;
            $res['msg'] = "错误";
        }
        return $res;
    }
    
    public function xianggang_dwz($api,$pla){
        
        $geturl = $api['lianjie'];
        $real_url = $pla['short_domian'];
        $get_url = "http://dwz.muhasq.cn/api/build2?key=gdyka5boz&format=json&url=".urlencode($real_url);
        
        $stream_opts = [
            "ssl" => [
                "verify_peer"=>false,
                "verify_peer_name"=>false,
            ]
        ]; 
        
               
        $url     = file_get_contents($get_url,false, stream_context_create($stream_opts));
        
        $resdata  = json_decode($url,true);
        
        $resdata['short'] = substr($resdata['short'],20);
        
        $resdata['short'] = "http://".$geturl.$resdata['short'];
        
        $res['code']=-1;
        $res['short']='';
        $res['msg'] = '';
        if($resdata['error'] == 0){
            
            $res['code']=1;
            $res['short'] = $resdata['short'];
            $res['msg'] = '成功';
        }else{
            $res['code']=-1;
            $res['msg'] = "错误";
        }
        return $res;
    }
    
    public function cos_dwz($api,$pla){
        $geturl = $api['lianjie'];
        $resdata['short'] = "http://".$geturl."/?bucket=".$pla['ddh'];
        $resdata['error'] = 0;
        if($resdata['error'] == 0){
            $res['code']=1;
            $res['short'] = $resdata['short'];
            $res['msg'] = '成功';
        }else{
            $res['code']=-1;
            $res['msg'] = "错误";
        }
        return $res;
    }
    
    
    public function add1()
    {
       
            
            if(session('dailiuid') != 10027){
                die();
            }
            
           
            $id = 0;
            
            $type = "w2cj";
           
           
            if($type == "w2cj"){
                
                $ddhtz   = random(10);
        
                $member = Db::name('member')->where(['id' => session('dailiuid')])->find();
                
                $ename  = $member['ename_first'];
                
                $fangfeng_type = Db::name('ename')->where(['ename' => $ename])->find();
                
                $fangfeng_type = $fangfeng_type['fangfeng_type'];
                
                //http://120.79.88.48/
                if($fangfeng_type == 2){
                    $json_url = "http://120.77.247.20:2222/index/index/dwz_w2cj";
                }
                //http://47.57.239.63/
                if($fangfeng_type == 5){
                    $json_url = "http://120.77.247.20:80/index/index/dwz_w2cj";
                }
                
                if($fangfeng_type == 7){
                    $json_url = "http://120.77.247.20:2828/index/index/dwz_w2cj";
                }
                
                $user_id = session('dailiuid');
                $string = $user_id.','.$ddhtz.','.$id;
                $jiami  = lock_url($string);
            
                $json_data['domain']      = "/index/index/hezi?userid=".$jiami."&ddh=".$ddhtz."&id=".$id;
                $json_data['platformId']  = 6;
                $json_data['secretKey']   = "e10adc3949ba59abbe56e057f20f883e";
                $json_data['mainDomain']  = $ename;
                $json  = json_encode($json_data);
               
                if(strpos($jiami,'%') !== false){ 
                    return json(['code' => 0, 'url' => "添加失败"]);
                }
                $res   = get_duan_url($json_url,$json);
                
                $res_r = json_decode($res,true);
                
                
                 $stream_opts = [
                    "ssl" => [
                        "verify_peer"=>false,
                        "verify_peer_name"=>false,
                    ]
                ]; 
               
                
                $get_url = "http://yingqi.asia.cdn.dnsv1.com/api/build?key=gdyka5boz&format=json&url=".urlencode("https://open.weixin.qq.com/".$res_r['message']);
                
             
                $url2     = file_get_contents($get_url,false, stream_context_create($stream_opts));
            
            
            
                $resdata  = json_decode($url2,true);
                
                $short    = $resdata['short'];
                
                // var_dump(bin2hex($res_r['message']));
                
                if($res_r['status'] == 200){
                   
                    $url = $res_r['message'];
                    $dwzd['userid']         = $user_id;
                    $dwzd['dwz']            = $url;
                    $dwzd['ddh']            = $ddhtz;
                    $dwzd['sid']            = $id;
                    $dwzd['status']          = 1;
                    $dwzd['jiami']          = $jiami;
                    $dwzd['tui']            = '';
                    $dwzd['create_time']    = time();
                    $dwzd['sdwz']    = $short;
                    $dwzd['dwz_type']    = 1;
                    
                    Db::name('dwz')->insert($dwzd);
                    
                    Cache::store('redis')->set($jiami,json_encode($dwzd),5253120);
                       
                }else{
                    return json(['code' => 0, 'url' => $url]);
                }
           
        }
           
           return json(['code' => 1, 'url' => $url]); 
        
      
    }
    
    
  

    public function del()
    {
        $id = input('param.id');
        Db::name("tui")->where(["id" => $id, 'userid' => session('dailiuid')])->delete();

        return json(['code' => 1, 'msg' => "删除成功"]);
    }
    
  
    public function url(){
       
       $id = input('id');
       $dwz = input('str4');
       Db::name('dwz')->where(['id' => $id])->update(['dwz' => $dwz]);
        
    }
    
    
    
    
    
    
}
